//
//  PracticeViewController.m
//  HotWord
//
//  Created by Rainbow on 8/25/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "PracticeViewController.h"
#import "MainMenuViewController.h"
#import "CacheData.h"
#import "HotWordAppDelegate.h"
#import "WrongWordView.h"
#import "Meaning.h"

@implementation PracticeViewController
@synthesize backBtn;
//@synthesize MainToolBar = _MainToolBar;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        //[self.view bringSubviewToFront: [self.view viewWithTag:555]];
    }
    return self;
}

- (void)dealloc
{
    [_bgView release];
    [super dealloc];
}

-(void)backToMenu
{
    [[CacheData sharedData] syncLetterStatus];
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    introduceFlag=0;
    
    for (int i = 0; i < 5; i++) {
        PracticingView *practiceView = [[[NSBundle mainBundle] loadNibNamed:@"PracticingView" owner:self options:nil] objectAtIndex:0];
        practiceView.delegate = self;
        practiceView.frame = CGRectMake(0, 25 + i*79, 320, 65);
    
        [self.bgView addSubview:practiceView];
        
        Word *word;
        if (i == 0) {
            word = [[CacheData sharedData] getWordByPosition:HotWordGetWordPositionCurrent];
            [practiceView setWordData:word];
        }
        else
        {
            if (_isFinish) {
                [practiceView setWordData:nil];
                return;
            }
            Word *lastWord = [[CacheData sharedData].selectedLetter.words lastObject];
            word = [[CacheData sharedData] getWordByPosition:HotWordGetWordPositionNext];
            [practiceView setWordData:word];
            if (word == lastWord) {
                _isFinish = YES;
            }
        }
    }
    self.bgView.top = [UIScreen mainScreen].bounds.size.height;
    [UIView animateWithDuration:0.6
                     animations:^{
                         self.bgView.top = 0;
                     }];
}

-(void)viewWillAppear:(BOOL)animated
{  
    HotWordAppDelegate *appDelegate= (HotWordAppDelegate *)[UIApplication sharedApplication].delegate;

    UIButton *wrongWordBtn=[[[UIButton alloc] init] autorelease];
    wrongWordBtn.frame=CGRectMake(110, 20, 100, 100);
    wrongWordBtn.tag=77;
    [wrongWordBtn addTarget:self action:@selector(wrongWord) forControlEvents:UIControlEventTouchUpInside];
    [wrongWordBtn setImage:[UIImage imageNamed:@"wrongWordShow.png"] forState:UIControlStateNormal];
    [wrongWordBtn setImage:[UIImage imageNamed:@"wrongWordShow.png"] forState:UIControlStateHighlighted];
    [wrongWordBtn setBackgroundImage:[UIImage imageNamed:@"tap_light.png"] forState:UIControlStateHighlighted];
    wrongWordBtn.alpha=0.0f;
    

    [appDelegate.MainToolBar addSubview:wrongWordBtn];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.3f];
    
    wrongWordBtn.alpha=1.0f; 

    [UIView commitAnimations];
    
}


-(void)wrongWord
{   
    if(introduceFlag==0)
        
    {
        UIImageView *backImageView=[[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"black.png"]] autorelease];
        backImageView.frame=CGRectMake(0, 0, 320, 480);
        backImageView.alpha=0.0f;
        backImageView.userInteractionEnabled=YES;
        backImageView.tag=188;
        
        [self.view addSubview:backImageView];
        
        WrongWordView *wrongWordView = [[[NSBundle mainBundle] loadNibNamed:@"WrongWordView" owner:self options:nil] objectAtIndex:0];
        wrongWordView.frame = CGRectMake(20, 480, 274, 413);
        wrongWordView.tag = 199;
        [self.view addSubview:wrongWordView];
        
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDuration:0.3f];
        [self.view viewWithTag:188].alpha=0.3f;
        [[self.view viewWithTag:199] setFrame:CGRectMake(20, 30, 274, 413)];
        [UIView commitAnimations];
        introduceFlag = 1;
        
    }
    else if(introduceFlag ==1)
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDuration:0.3f];
        [self.view viewWithTag:188].alpha=0.0f;
        [[self.view viewWithTag:199]setFrame:CGRectMake(20, 480, 274, 413)];
        [UIView setAnimationDidStopSelector:@selector(removeWrongWordList)];
        [UIView commitAnimations];
        
        
        introduceFlag = 0;
        
    }
}

-(void)removeWrongWordList
{
    
    [[self.view viewWithTag:188] removeFromSuperview];
    [[self.view viewWithTag:199] removeFromSuperview];
    
}

- (void)PracticeViewChooseRight:(PracticingView *)practicingView
{
    if (_isFinish) {
        [practicingView setWordData:nil];
        return;
    }
    Word *word = [[CacheData sharedData] getWordByPosition:HotWordGetWordPositionNext];
    Word *lastWord = [[CacheData sharedData].selectedLetter.words lastObject];
    [practicingView setWordData:word];
    if (word == lastWord) {
        _isFinish = YES;
    }
}

- (void)viewDidUnload
{
    [self setBgView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
